package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.vo.CatalogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;
    @Autowired
    private StrategyRepository strategyRepository;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
        strategyCatalogRepository.deleteById(id);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public StrategyCatalog get(String id) {
        return strategyCatalogRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyCatalog> lists() {
        return strategyCatalogRepository.findAll();
    }

    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {
        //创建查询对象
        Query query = new Query();
        //查询出查询对象条数
        long count = mongoTemplate.count(query, StrategyCatalog.class);
        if (count ==0){
            return Page.empty();
        }
        //条件
        //分页
        Pageable pageable =PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");
        query.with(pageable);
        //查询页面分页排序后的所有数据
        List<StrategyCatalog> list = mongoTemplate.find(query, StrategyCatalog.class);
        return new PageImpl<>(list,pageable,count);
    }

    @Override
    public List<CatalogVo> catalogLists() {
        //创建一个空的集合,放数据
        List<CatalogVo> list =new ArrayList<>();
        //利用聚合查询数据
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
                Aggregation.group("destName").
                        push("name").as("names").
                        push("id").as("ids")
        );
        //利用,MongoDB中的类
        AggregationResults<Map> result = mongoTemplate.aggregate(agg,Map.class);
        List<Map> datas = result.getMappedResults(); //将结果转化成list<map>   map描述一行数据
        //StrategyCatalog得到的是所有数据,还是没有分开的
        for (Map data : datas) {
            //创建vo类来接收数据
            CatalogVo vo = new CatalogVo();
            //集合中区域的名字
            vo.setDestName(data.get("_id").toString());
            //创建两个集合放names和ids
            List<Object> namesList=(List<Object>)data.get("names");
            List<Object> idsList=(List<Object>)data.get("ids");
            //创建一个集合放分类
            List<StrategyCatalog> listsCatalog=new ArrayList<>();
            //因为他们的长度一样
            for (int i=0;i<namesList.size();i++){
                //创建一个分类对象
                StrategyCatalog strategyCatalog = new StrategyCatalog();
                //把数据放入数据中
                strategyCatalog.setName(namesList.get(i).toString());
                strategyCatalog.setId(idsList.get(i).toString());
                //把对象放入集合中
                listsCatalog.add(strategyCatalog);
            }
            vo.setListsCatalog(listsCatalog);
            //把所有数据放入自定义的对象中
            list.add(vo);
        }
        return list;
    }

    @Override
    public List<StrategyCatalog> queryDestByRegionId(String destId) {
        //通过目的地查询分类
        List<StrategyCatalog> strategyCatalogList= strategyCatalogRepository.findByDestId(destId);
        //处理第二层
        for (StrategyCatalog strategyCatalog : strategyCatalogList) {
            //通过分类id查询出他对应的攻略明细
            List<Strategy> strategyList=strategyRepository.findByCatalogId(strategyCatalog.getId());
            strategyCatalog.setStrategies(strategyList);
        }
        return strategyCatalogList;
    }


}
